package customer.zspt.cn.customer.components.chat.ChatOrderContent;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import customer.zspt.cn.customer.Adapter.OrderProLsAdapter;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.OrderProLsDdao;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.BaseView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.CheckBoxView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.DateIntervalStartView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.MoreEtInputView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.NumberInputView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.OneEditInputView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.RadioBoxView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.SeekBarView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.SwitchView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.TimeADateView;
import customer.zspt.cn.customer.components.chat.ChatOrderContent.OrderElementPg.TimeIntervalStartView;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.utils.GetAllInfoUtils;
import customer.zspt.cn.customer.utils.SPUtils;
public class orderContentActivity extends Activity {
    @ViewInject(R.id.order_Content)
    private LinearLayout order_Content;
    @ViewInject(R.id.loading)
    private View loading;
    @ViewInject(R.id.order_ls)
    private ListView order_ls;
    @ViewInject(R.id.top_ll)
    LinearLayout top_ll;
    private Gson gson;
    private String User;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ordercontent);
        ViewUtils.inject(this);
         User = getIntent().getStringExtra("User");
        String ORDERPROP = SPUtils.getString(this,constant.ORDERPROP,"");
        gson = new Gson();
        OrderProLsDdao orderProLsDdao = gson.fromJson(ORDERPROP, OrderProLsDdao.class);
        final List<OrderProLsDdao.OrderProLs> data = orderProLsDdao.data;
        OrderProLsAdapter adapter = new OrderProLsAdapter(data,this);
        order_ls.setAdapter(adapter);
        order_ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                setOrderData(data.get(position).orderpropId+"");
                order_ls.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if(order_ls.getVisibility()==View.GONE){
            order_ls.setVisibility(View.VISIBLE);
            loading.setVisibility(View.VISIBLE);
            order_Content.removeAllViews();
        }else{
            finish();
        }
    }
    /**请求数据*/
    private void setOrderData(String orderpropId){
        String token = SPUtils.getString(this,"token","");
        GetAllInfoUtils.infoUtils.getFriendInfo(constant.AllMSG+
                "/orderprop/info/"+orderpropId,token,this);
        GetAllInfoUtils.infoUtils.monitoringLinster(new GetAllInfoUtils.ReadyData() {
            @Override
            public void success(String data) {
                pullData(data);
            }
            @Override
            public void fail() {

            }
        });
    }
    /**解析数据*/
    private void pullData(String data) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loading.setVisibility(View.GONE);
            }
        });
        try{
            JSONObject resultData = new JSONObject(data);
            JSONObject arr = resultData.getJSONObject("data");
            String WeNeedData = arr.getString("orderpropContent");
            SePullData(WeNeedData);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void SePullData(String weNeedData) {
        try{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        addDefaultView();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
            JSONArray resultData = new JSONArray(weNeedData);
            for(int i=0;i<resultData.length();i++){
                JSONObject data = (JSONObject)resultData.get(i);
                String type = (String)data.get("type");
                addViewFunction(type,data);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void addDefaultView() throws JSONException {
        LinearLayout top_ll = (LinearLayout) findViewById(R.id.top_ll);
        String title = "";
        title = "工单主题";
        BaseView view = new OneEditInputView(this,title,"");
        top_ll.addView(view);
        title = "优先级";
        String options = "[{'label':'正常','value':'正常'}," +
                "{'label':'急','value':'急'}," +
                "{'label':'加急','value':'加急'}]";
        JSONArray arr = new JSONArray(options);
        view = new RadioBoxView(this,title,arr,"");
        top_ll.addView(view);
        title = "技能组";
        String option = "[{'label':'售前','value':'售前'}," +
                "{'label':'售后','value':'售后'}," +
                "{'label':'技术','value':'技术'}]";
        JSONArray arrs = new JSONArray(option);
        view = new RadioBoxView(this,title,arrs,"");
        top_ll.addView(view);
        TimeADateView timeADateView = new TimeADateView(this);
        top_ll.addView(timeADateView);
        View Abouut_View = View.inflate(this,R.layout.order_about_user,null);
        TextView about_user = Abouut_View.findViewById(R.id.about_user);
        about_user.setText(User);
        top_ll.addView(Abouut_View);
    }
    private void addViewFunction(String type, JSONObject data) throws JSONException {
        String title = (String) data.getString("label");
        BaseView view = null;
        String defaultValues = null;
        JSONArray arr = null;
        JSONArray defaultArr = null;
        switch (type){
            case "switch":
                boolean defaultValue = data.getBoolean("default");
                view = new SwitchView(this,title,defaultValue);
                break;
            case "number":
                defaultValues = data.getString("default");
                view = new NumberInputView(this,title,defaultValues);
                break;
            case "timerange":
                defaultArr = data.getJSONArray("default");
                view = new TimeIntervalStartView(this,title,defaultArr,false,"");
                break;
            case "input":
                defaultValues = data.getString("default");
                view = new OneEditInputView(this,title,defaultValues);
                break;
            case "checkbox":
                arr = data.getJSONArray("options");
                defaultArr = data.getJSONArray("default");
                view = new CheckBoxView(this,title,arr,defaultArr);
                break;
            case "select":
                arr = data.getJSONArray("options");
                defaultValues = data.getString("default");
                view = new RadioBoxView(this,title,arr,defaultValues);
                break;
            case "slider":
                defaultValues = data.getString("default");
                view = new SeekBarView(this,title,defaultValues);
                break;
            case "textarea":
                defaultValues = data.getString("default");
                view = new MoreEtInputView(this,title,defaultValues);
                break;
            case "checkbutton":
                arr = data.getJSONArray("options");
                defaultArr = data.getJSONArray("default");
                view = new CheckBoxView(this,title,arr,defaultArr);
                break;
            case "daterange":
                defaultArr = data.getJSONArray("default");
                view = new DateIntervalStartView(this,false,title,defaultArr,"");
                break;
            case "date":
                defaultValues = data.getString("default");
                view = new DateIntervalStartView(this,true,title,null,defaultValues);
                break;
            case "time":
                defaultValues = data.getString("default");
                view = new TimeIntervalStartView(this,title,null,true,defaultValues);
                break;
        }
        final BaseView finalView = view;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(finalView!=null){
                    order_Content.addView(finalView);
                }
            }
        });

    }
}
